package com.example.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.bean.ServiceBean;
import com.example.home.bus.BusMainActivity;
import com.example.home.government.acivity.GovernmentMainActivity;
import com.example.home.hospital.activity.HospitalMain;
import com.example.home.library.LibraryMainActivity;
import com.example.home.metro.MetroMainActivity;
import com.example.home.park.CatActivity;
import com.example.home.rubbish.RubbishMainActivity;
import com.example.home.volunteer_service.VolunteerServiceMainActivity;

import java.util.List;

public class ServletAdapter extends RecyclerView.Adapter<ServletAdapter.MyServer>{
    private Context context ;
    private List<ServiceBean.RowsBean> list;
    private View view;
    private String url = "http://124.93.196.45:10001";

    public ServletAdapter(Context context, List<ServiceBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyServer onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充试图
        view = LayoutInflater.from(context).inflate(R.layout.item_sever,null);
        return new MyServer(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyServer holder, int position) {
        if(position !=9){
            Glide.with(context).load(url+list.get(position).getImgUrl()).into(holder.sever_tv_img);
            holder.sever_iv_text.setText(list.get(position).getServiceName());

            //进入停哪儿页面
            if (list.get(position).getId()==17){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, CatActivity.class);
                        context.startActivity(intent);
                    }
                });
            }

            //进入图书馆页面
            if (list.get(position).getId()==30){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, LibraryMainActivity.class);
                        context.startActivity(intent);
                    }
                });
            }

            //进入政府热线页面
            if (list.get(position).getId()==29){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, GovernmentMainActivity.class);
                        context.startActivity(intent);
                    }
                });
            }
            //进入政府热线页面
            if (list.get(position).getId()==5){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, HospitalMain.class);
                        context.startActivity(intent);
                    }
                });
            }
            //进入垃圾分类页面
            if (list.get(position).getId()==31){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, RubbishMainActivity.class);
                        context.startActivity(intent);
                    }
                });
            }

            //智慧巴士
            if (list.get(position).getId()==3){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, BusMainActivity.class);
                        context.startActivity(intent);
                    }
                });
            }

            //志愿服务
            if (list.get(position).getId()==32){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, VolunteerServiceMainActivity.class);
                        context.startActivity(intent);
                    }
                });
            }
            //地铁
            if (list.get(position).getId()==2){
                holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, MetroMainActivity.class);
                        context.startActivity(intent);
                    }
                });
            }

        }else {
            Glide.with(context).load(url+list.get(9).getImgUrl()).into(holder.sever_tv_img);
            holder.sever_iv_text.setText("更多服务");
            holder.sever_iv_text.setTextSize(14);
            holder.sever_tv_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("bov_action1");
                    context.sendBroadcast(intent);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return 10;
    }

    class  MyServer extends RecyclerView.ViewHolder{

        private final RecyclerView recyclerView;
        private final TextView sever_iv_text;
        private final ImageView sever_tv_img;

        public MyServer(@NonNull View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.re_servlet);
            sever_iv_text = itemView.findViewById(R.id.sever_iv_text);
            sever_tv_img = itemView.findViewById(R.id.sever_tv_img);
        }
    }
}

